from flask import Blueprint, request, make_response, redirect
from common.libs.Helper import ops_renderJSON, ops_renderErrJSON, ops_render
from common.models.car_order import CarOrder
from common.libs.DataHelper import getCurrentTime
from application import app, db
import json, requests



car_page = Blueprint("car_page", __name__)


@car_page.route('/carorder', methods=['GET', 'POST'])
def carorder():
    req = request.values
    date = req['date'] if "date" in req else ""
    time = req['time'] if "time" in req else ""
    place = req['place'] if "place" in req else ""
    num = req['num'] if "num" in req else ""
    uid = req['uid'] if "uid" in req else ""
    reason = req['reason'] if "reason" in req else ""

    if len(date) < 1:
        return ops_renderErrJSON(msg="请选择日期")

    if len(place) < 1:
        return ops_renderErrJSON(msg="请输入用车地点")

    if len(num) < 1 :
        return ops_renderErrJSON(msg="请输入用车人数")

    if len(reason) < 1 :
        return ops_renderErrJSON(msg="请输入事由")

    else:
        Order = CarOrder()
        Order.cartime=date+" "+time
        Order.carplace = place
        Order.carnum = num
        Order.carreason = reason
        Order.uid = uid
        Order.mark = 1
        Order.created_time = Order.updated_time = getCurrentTime()

        db.session.add(Order)
        db.session.commit()

        return json.dumps({"code": 200, "msg": "申请成功", }, indent=4,
                          sort_keys=True, default=str,
                          ensure_ascii=False)

